var over = 0;
var action = 0;
var _order_by = null;

var thisPage = {
    first: 0,
    // 初始化页面
    OnPageLoad: function () {
        $("abbr.page_indicator_block,#wrapper_navbar").attr("_msh_page", 1);
        thisPage.FetchForPage(1);
    },
    // 得到分页数据
    GetFilter: function (pageIndex) {
        if (pageIndex !== 0) {
            $("abbr.page_indicator_block,#wrapper_navbar").attr("_msh_page", pageIndex);
        }
        var item = {};
        item['Limit'] = parseInt($("#page-size").val());
        item['PageIndex'] = parseInt($("abbr.page_indicator_block,#wrapper_navbar").attr("_msh_page"));
        item['OrderBy'] = _order_by;

        return JSON.stringify(item);
    },
    // 得到数据列表信息
    FetchForPage: function (pageIndex) {
        $.ajax({
            type: "POST",
            url: parent.basePath + '/admin/role/fetchForPage.html',
            timeout: 30 * 1000,
            data: thisPage.GetFilter(pageIndex),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            if (data.Success === true) {
                thisPage.BuildDataTable(data);
            }
        }).fail(function (data) {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(function () {
            if (thisPage.first === 0) {
                thisPage.first = 1;
            }
        });
    },
    // 构建数据列表
    BuildDataTable: function (result) {
        var current_page = result.PageIndex;
        // 设置翻页控件
        if (result.TotalPages > 0) {  //总页数
            $("#wrapper_navbar").data("swPagination").options.max_page = result.TotalPages;
        } else {
            $("#wrapper_navbar").data("swPagination").options.max_page = 1;
        }
        if (result.PageIndex > 0) { //当前页数
            $("#wrapper_navbar").data("swPagination").options.current_page = result.PageIndex;
            $("#wrapper_navbar").data("swPagination").setPage(result.PageIndex);
        } else {
            $("#wrapper_navbar").data("swPagination").options.current_page = 1; //当前页数
            $("#wrapper_navbar").data("swPagination").setPage(1);
        }

        $("#tabData01 tbody").empty();
        // 得到数据列表
        result = result.DataList;
        if (result === null) {
            $('#loading').hide();
            return false;
        }
        var content = '';
        for (var i = 0; i < result.length; i++) {
            var id = result[i].roleId;
            var sequence = i + 1 + (current_page - 1) * parseInt($("#page-size").val());
            content += '<tr _mash_id="' + id + '">';
            content += '<td class="rowNumber">' + sequence + '</td>';                   // 序号
            content += '<td><span title="编辑" class="edit" onclick="thisPage.DoEdit(' + id + ',$(this))"><i class="fa-all fa-pencil fa-1"></i></span></td>';
            content += '<td><span title="删除" class="trash" onclick="thisPage.DoDelete(' + id + ');"><i class="fa-all fa-trash-o fa-1"></i></span></td>';
            content += '<td>' + formatNullData(result[i].roleNo) + '</td>';            // 角色号
            content += '<td>' + formatNullData(result[i].roleName) + '</td>';          // 角色名称
            content += '<td style="border-right:none;">' + formatNullData(result[i].description) + '</td>';
            content += '</tr>';
            $("#tabData01 tbody").append(content);
            content = '';
        }
        $("#tabData01 tbody tr:odd").removeClass().addClass("odd");
        $("#tabData01 tbody tr:even").removeClass().addClass("even");

        setOneTableColor("tabData01");

        // 设置最后一行的样式
        var $lastLine = $('#tabData01 tbody tr:last-child');
        $lastLine.find('td').removeClass('last').addClass('last');

        $('#loading').hide();
    },
    // 增加系统成员操作
    AddRole: function () {
        location.replace(parent.basePath + '/admin/role/edit.html?id=0&action=1');
    },
    // 编辑
    DoEdit: function (id) {
        location.replace(parent.basePath + '/admin/role/edit.html?id=' + id + '&action=2');
    },
    _RoleId: 0,
    // 删除系统成员操作
    DoDelete: function (roleId) {
        thisPage._RoleId = roleId;
        showModal(333, 140, '确认删除', '', null, null, 1, '请问，确定要删除当前记录吗？', thisPage.OnDelete);
    },
    //确认删除后处理
    OnDelete: function () {
        var instance = {};
        instance['roleId'] = thisPage._RoleId;
        instance['deletedBy'] = $("#UserId").val();

        $.ajax({
            type: "POST",
            url: parent.basePath + "/admin/role/delete.html",
            timeout: 30 * 1000,
            data: JSON.stringify(instance),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: function () {
            }
        }).done(function (data) {
            if (data > 0) {
                var currPage = $("abbr.page_indicator_block,#wrapper_navbar").attr("_msh_page");
                // 如果删除的是最后一页的最后一条，并且当前页不是首页,重画页面设为前一页
                if (parseInt($('#tabData01 tbody').find('tr').length) === 1 && currPage > 1) {
                    thisPage.FetchForPage(currPage - 1);
                } else {
                    thisPage.FetchForPage(currPage);
                }
            }
        }).fail(function (data) {
            parent.ajaxError(data, 1);
        }).always(function () {
            thisPage._RoleId = 0;
        });
    }
};

//-----------------------------------------------------------------------------
// 表格排序
//-----------------------------------------------------------------------------
function sortTable(order_by) {
    _order_by = order_by;
    thisPage.FetchForPage(1);
}

//-----------------------------------------------------------------------------
// 页面初始化
//-----------------------------------------------------------------------------
function onPageReady() {
    setWndTitle('角色一览');
    setViewPosition('<span>系统管理</span>角色一览');

    //设置分页控制
    $("#wrapper_navbar").swPagination({
        link_string: '/?page={page_number}',
        max_page: null,
        paged: function (page) {
            $("abbr.page_indicator_block,#wrapper_navbar").attr("_msh_page", page);
            thisPage.FetchForPage(1);
        }
    });

    // 设置表格排序字段
    /*$("#tabHead01").wxTableSorter({
     sortList: [[2, 1], [4, 1]], // 第一个表示列的序号，第二个表示排序种类
     headers: {2: {sorter: false}, 4: {sorter: false}},
     columnList: [[1, "MS.UserName"], [2, "E.DepartmentId"]],
     detailList: $("#tabData01"),
     sort: sortTable
     });*/

    thisPage.OnPageLoad();
}

// 画面大小或者位置移动
function onPageResize() {
    var h = $(window).height();
    var w = 1366; //Math.max($(window).width(), 1366);

    $("#spanPageSize").css({left: w - 140});
    $("#gridWrapper").css({left: 8, width: w - 16});
    $("#tabHead01").find("col").last().css({width: w - 20 - 388});
    $("#tabData01").find("col").last().css({width: w - 20 - 388});
    over = 1;
}